package cn.fourhundredandeighteen.controller;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.entity.Course;
import cn.fourhundredandeighteen.entity.StudentCourse;
import cn.fourhundredandeighteen.service.CourseService;
import cn.fourhundredandeighteen.service.StudentCourseService;
import cn.fourhundredandeighteen.service.TeacherService;
import cn.fourhundredandeighteen.service.impl.CourseServiceImpl;
import cn.fourhundredandeighteen.service.impl.StudentCourseServiceImpl;
import cn.fourhundredandeighteen.service.impl.TeacherServiceImpl;
import cn.fourhundredandeighteen.utill.AdminCheck;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.utill.ResultGenerator;

import java.util.List;

/**
 * @description:课程对象操作类
 * @author: liuyongqi
 * @time: 2020/12/20
 */
public class CourseController {
    /**课程添加
     * 老师
     * @param course
     * @return
     *  功能已添加
     */
    public Result addCourse(Course course){
        CourseService courseService1=new CourseServiceImpl();
        try {
            //教师验证
            int i = courseService1.setCourse(course);

            if (i > 0) {
                return ResultGenerator.successResult();
            } else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /**课程删除
     * 老师
     * @param courseId
     * @return
     * 功能已添加
     */
    public Result delCourse(Long courseId){
        CourseService courseService=new CourseServiceImpl();
        StudentController studentController=new StudentController();
        StudentCourseService studentCourseService=new StudentCourseServiceImpl();
        TeacherController teacherController=new TeacherController();
        try {
            //教师验证
            Course course1 = courseService.getCourse(courseId);
            if (!AdminCheck.adminCheck()) {
                if (!course1.getTeacherId().equals(UserInfo.Session))
                    return ResultGenerator.errorResult();
            }
            //删除课程关联
            List<StudentCourse> studentCourses = studentCourseService.selectCourseToStudent(courseId);
            for(StudentCourse studentCourse:studentCourses){
                //不管正确还是错误，正确说明有数据就会删除
                //删除课程关联
                Result result = studentController.cancelCourse(courseId, studentCourse.getStudentId());
                //删除课程成绩
                Result result1 = teacherController.delStudentScore(courseId, studentCourse.getStudentId());
            }
            //删除课程
            int i = courseService.delCourse(courseId);
            if (i > 0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 课程精确查询
     * @param courseId
     * @return
     * 功能已添加
     */
    public Result<Course> getCourseId(Long courseId){
        CourseService courseService=new CourseServiceImpl();
        try {
            Course course = courseService.getCourse(courseId);
            if (course==null)
                return ResultGenerator.errorResult();
            return ResultGenerator.successResult(course);
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }
    /**所有课程查询
     * All
     * @return
     * 功能已添加
     */
    public Result<List<Course>> getCourse(){
        CourseService courseService=new CourseServiceImpl();
        List<Course> courseAll = courseService.getCourseAll();
        if (courseAll != null)
            return ResultGenerator.successResult(courseAll);
        else
            return ResultGenerator.errorResult();
    }

    /**课程信息更新
     * 老师
     * @param course
     * @return 课程id
     * 功能已添加
     */
    public Result upCourse(Course course){
        CourseService service=new CourseServiceImpl();
        try {
            //教师验证
            Course course1 = service.getCourse(course.getCourseId());
            if (!AdminCheck.adminCheck()){
                if (!course1.getTeacherId().equals(UserInfo.Session))
                    return ResultGenerator.errorResult();
            }
            int i = service.upCourse(course);
            if (i>0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }

    }
}
